import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigInteger;


public class GenProof {
	static final int p = 1031;
	static final int blockLen = 1024; 
	static final int g1 = 2;
	static final int g2 = 2;
	
	private int v;
	private int w;
	private int u;
	
	private String miu;
	private String sigma;
	private int R;
	private int r;
	
	private int[] m;
	
	public GenProof(int v, int w, int u)
	{
		 R = r = 0;
		sigma = "";
		miu  ="";
		this.u = u;
		this.v = v;
		this.w = w;
	}
	
	public GenProof()
	{
		v = w = u =   R = r = 0;
		sigma =miu = "";
	}
	
	public void  generateR() {
		r = (int)(Math.random()*p);
	}
	
	public void calculateR() {
		R = (w*r);
	}
	
	public void readFile(String fileName, int[] checkBlock) throws IOException {
		m = new int[checkBlock.length];
		
		RandomAccessFile rFile = null;
		rFile = new RandomAccessFile(fileName,"r");
		
		for (int i = 0; i < checkBlock.length; i++) {
			int bIndex = (checkBlock[i] -1)*blockLen;
			rFile.seek(bIndex);
			byte[] buffer = new byte[blockLen];
			int byteRead = 0;
			byteRead=rFile.read(buffer);
			assert(byteRead==-1);
			System.out.write(buffer,0,byteRead);
			System.out.print("\n");
			
			BigInteger mi = new BigInteger("0");
			for (int j = 0 ; j < byteRead ; j++){
				mi=mi.add(BigInteger.valueOf(buffer[j]));
				mi = mi.mod(BigInteger.valueOf(p));
				mi=mi.shiftLeft(8);
			}
			
			System.out.println(mi);
			mi = mi.mod(BigInteger.valueOf(p));
			m[i] = mi.intValue();
		}

	}
	
	public void calculateMiu(int[][] chal, String fileName) throws IOException {
		readFile(fileName, chal[0]);
		BigInteger miuDash = new BigInteger("0");
		for (int i = 0; i < chal[0].length; i++) {
			miuDash = miuDash.add(BigInteger.valueOf((long)(chal[1][i]*m[i]))) ;
			//miuDash = miuDash.mod(BigInteger.valueOf(p));

		}
		BigInteger temp = new BigInteger("0").add(BigInteger.valueOf(r));
		miuDash = miuDash.add(temp.multiply(BigInteger.valueOf(HashMethod.gethHash(R))));
		//miuDash = miuDash.mod(BigInteger.valueOf(p));

		miu = miuDash.toString();
	}
	
	//public void calculateSigma(int[][] chal,  int[] signature) {
	public void calculateSigma(int[][] chal, BigInteger[] signature){
		BigInteger sig = new BigInteger("0");
		for (int i = 0; i < chal[0].length; i++) {
			//sig = sig.add(BigInteger.valueOf(signature[chal[0][i] -1] * chal[1][i]));
			BigInteger a = signature[chal[0][i]-1];
			BigInteger b = new BigInteger(String.valueOf(chal[1][i]));

			sig = sig.add(a.multiply(b));
			//sigma = sigma % p;
		}
		sigma = sig.toString();
	}
	
	//public void genProof(int[][] chal,  int[] signature, String fileName) throws IOException {
	public void genProof(int[][] chal, BigInteger[] signature, String fileName) throws IOException{
		generateR();
		calculateR();
		
		calculateMiu(chal, fileName);
		
		calculateSigma(chal, signature);
	}
	
	
	public int getV() {
		return v;
	}

	public void setV(int v) {
		this.v = v;
	}

	public int getW() {
		return w;
	}

	public void setW(int w) {
		this.w = w;
	}

	public int getU() {
		return u;
	}

	public void setU(int u) {
		this.u = u;
	}

	public String getMiu() {
		return miu;
	}

	public void setMiu(String miu) {
		this.miu = miu;
	}

	public String getSigma() {
		return sigma;
	}

	public void setSigma(String sigma) {
		this.sigma = sigma;
	}

	public int getR() {
		return R;
	}

	public void setR(int r) {
		R = r;
	}
	
}
